﻿Imports TNCDAL

Public Class ucTransportFee
    Dim db As New TanNamChinhDataContext
    Private m_ID_Cont As Integer
    Private m_uc As UserControl

    Public Property ID_Cont As Integer
        Get
            Return m_ID_Cont
        End Get
        Set(ByVal value As Integer)
            m_ID_Cont = value
        End Set
    End Property

    Public Sub New(ByVal ID_Cont As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_ID_Cont = ID_Cont
        KhoiTaodtgContFee(ID_Cont)
        AddComboBoxColumns()
        AddComboBoxColumnsCurency()
        dtgContFee.ReadOnly = True
    End Sub

    Public Sub New(ByVal ID_Cont As Integer, ByVal uc As UserControl, ByVal TieuDe As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_ID_Cont = ID_Cont
        KhoiTaodtgContFee(ID_Cont)
        AddComboBoxColumns()
        AddComboBoxColumnsCurency()
        dtgContFee.ReadOnly = True
        m_uc = uc
        lblTieuDe.Text = TieuDe
    End Sub

    Private Sub ucTransportFee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub KhoiTaodtgContFee(ByVal ID_Cont As Integer)
        'Kiem tra ton tai trong tblContFee chưa?
        'Neu chua Insert các loại phí Transport vào tblContFee
        Dim c_contfee = (From p In db.tblContFees
                         Where p.ID_Cont = ID_Cont
                         Select p).Count
        If c_contfee = 0 Then
            Dim fee_transport = From p In db.tblFees
                                Where p.ID_FeeType = 3
                                Select p

            For Each fee As tblFee In fee_transport
                Dim cont_fee As New tblContFee
                cont_fee.ID_Cont = ID_Cont
                cont_fee.ID_Fee = fee.ID_Fee
                cont_fee.Price = 0
                cont_fee.UnitPrice = 0
                cont_fee.Quantity = 1
                cont_fee.ID_Currency = 1 'VND
                db.tblContFees.InsertOnSubmit(cont_fee)
                db.SubmitChanges()
            Next

        End If

        Dim contFee = From p In db.tblContFees
                     Where p.ID_Cont = ID_Cont
                     Select New With {p.ID_ContFee, p.ID_Fee, p.ID_Payee, p.tblFee.FeeName, p.UnitPrice, p.Quantity, p.Price, p.InvoiceNo, p.Description, p.ID_Currency}

        dtgContFee.DataSource = contFee

        FormatdtgContFee()
        'Tinh TongTien theo Cont
        Dim TongTien As Double = 0

        For i As Integer = 0 To dtgContFee.RowCount - 1
            TongTien += dtgContFee.Rows(i).Cells("Price").Value
        Next
        lblTotal.Text = TongTien.ToString("#,###")
    End Sub

    Public Sub FormatdtgContFee()
        dtgContFee.RowHeadersWidth = 20
        dtgContFee.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        'dtgContFee.RowTemplate.Height = 25

        dtgContFee.MultiSelect = False
        'dtgContFee.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        'dtgContFee.ReadOnly = True
        dtgContFee.BackgroundColor = Color.White
        dtgContFee.AllowUserToAddRows = False
        dtgContFee.AllowUserToDeleteRows = False
        dtgContFee.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells


        dtgContFee.Columns("ID_ContFee").Visible = False
        dtgContFee.Columns("ID_Fee").Visible = False
        dtgContFee.Columns("ID_Payee").Visible = False
        dtgContFee.Columns("ID_Currency").Visible = False

        'dtgContFee.Columns("ID_Cont").Visible = False
        'dtgContFee.Columns("ID_AdvanceVoucher").Visible = False
        'dtgContFee.Columns("ID_ReceiptVoucher").Visible = False
        'dtgContFee.Columns("ID_PaymentVoucher").Visible = False
        'dtgContFee.Columns("DriverName").Visible = False
        'dtgContFee.Columns("ID_FeeType").Visible = False
        'dtgContFee.Columns("FeeAbbreviated").Visible = False
        'dtgContFee.Columns("PhoneNo").Visible = False
        'dtgContFee.Columns("Address").Visible = False
        'dtgContFee.Columns("ID_Manager").Visible = False

        With dtgContFee.Columns("Price")
            .HeaderText = "Thành tiền"
            .Width = 100
            .DefaultCellStyle.Format = "#,###"
            .ReadOnly = True
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With
        With dtgContFee.Columns("UnitPrice")
            .HeaderText = "Đơn giá"
            .Width = 100
            .DefaultCellStyle.Format = "#,###"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With
        With dtgContFee.Columns("Quantity")
            .HeaderText = "Số lượng"
            .Width = 80
            .DefaultCellStyle.Format = "#,###"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With
        With dtgContFee.Columns("InvoiceNo")
            .HeaderText = "Số hóa đơn"
            .Width = 100
        End With
        With dtgContFee.Columns("Description")
            .HeaderText = "Ghi chú"
            .Width = 100
        End With
        With dtgContFee.Columns("FeeName")
            .HeaderText = "Loại phí"
            .Width = 150
            .ReadOnly = True
        End With
    End Sub

    Private Sub AddComboBoxColumns()
        Dim comboboxColumn As DataGridViewComboBoxColumn
        comboboxColumn = CreateComboBoxColumn()
        SetAlternateChoicesUsingDataSource(comboboxColumn)
        comboboxColumn.HeaderText = "Người đóng tiền"
        dtgContFee.Columns.Add(comboboxColumn)

        'comboboxColumn = CreateComboBoxColumn()
        'SetAlternateChoicesUsingItems(comboboxColumn)
        'comboboxColumn.HeaderText = _
        '    "TitleOfCourtesy (via Items property)"
        '' Tack this example column onto the end.
        'DataGridView1.Columns.Add(comboboxColumn)
    End Sub

    Private Function CreateComboBoxColumn() As DataGridViewComboBoxColumn
        Dim column As New DataGridViewComboBoxColumn()

        With column
            .DataPropertyName = "ID_Payee"
            .HeaderText = "Người đóng tiền"
            .DropDownWidth = 160
            .Width = 120
            .MaxDropDownItems = 3
            .FlatStyle = FlatStyle.Flat
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
        End With
        Return column
    End Function

    Private Sub SetAlternateChoicesUsingDataSource(ByVal comboboxColumn As DataGridViewComboBoxColumn)
        Dim driver = From p In db.tblDrivers
                       Select p
        With comboboxColumn
            .DataSource = driver
            .ValueMember = "ID_Driver"
            .DisplayMember = "DriverName"
        End With
    End Sub

    Public Function TaoColumscbb(ByVal colName As String) As DataGridViewComboBoxColumn
        Dim driver = From p In db.tblDrivers
                       Select p
        Dim cbbDriver As New DataGridViewComboBoxColumn
        cbbDriver.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
        cbbDriver.Name = colName
        cbbDriver.HeaderText = "Người đóng phí"
        cbbDriver.DataSource = driver
        cbbDriver.DisplayMember = "DriverName"
        cbbDriver.ValueMember = "ID_Driver"
        Return cbbDriver
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'Update tblConFee
        Dim TongTien As Double = 0

        For i As Integer = 0 To dtgContFee.RowCount - 1
            TongTien += dtgContFee.Rows(i).Cells("Price").Value
            Dim ID_ContFee As Integer
            ID_ContFee = dtgContFee.Rows(i).Cells("ID_ContFee").Value

            Dim contfee = (From p In db.tblContFees
                       Where p.ID_ContFee = ID_ContFee
                       Select p).Single

            contfee.ID_Payee = dtgContFee.Rows(i).Cells("ID_Payee").Value
            contfee.InvoiceNo = dtgContFee.Rows(i).Cells("InvoiceNo").Value
            contfee.Price = dtgContFee.Rows(i).Cells("Price").Value
            contfee.Description = dtgContFee.Rows(i).Cells("Description").Value
            contfee.UnitPrice = dtgContFee.Rows(i).Cells("UnitPrice").Value
            contfee.Quantity = dtgContFee.Rows(i).Cells("Quantity").Value
            contfee.ID_Currency = dtgContFee.Rows(i).Cells("ID_Currency").Value

            db.SubmitChanges()
        Next

        btnEdit.Enabled = True
        btnSave.Enabled = False
        btncancel.Enabled = False
        dtgContFee.ReadOnly = True
        KhoiTaodtgContFee(m_ID_Cont)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        btnSave.Enabled = True
        btncancel.Enabled = True
        btnEdit.Enabled = False
        dtgContFee.ReadOnly = False
        dtgContFee.Columns("Price").ReadOnly = True
    End Sub

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        btnEdit.Enabled = True
        btnSave.Enabled = False
        btncancel.Enabled = False
        dtgContFee.ReadOnly = True
        KhoiTaodtgContFee(m_ID_Cont)
    End Sub

    Private Sub dtgContFee_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgContFee.CellValueChanged
        Try
            If e.ColumnIndex = 5 Or e.ColumnIndex = 4 Then
                Dim DonGia As Double = 0
                Dim SoLuong As Integer = 0
                DonGia = dtgContFee.CurrentRow.Cells("UnitPrice").Value
                SoLuong = dtgContFee.CurrentRow.Cells("Quantity").Value
                dtgContFee.CurrentRow.Cells("Price").Value = DonGia * SoLuong
            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Sub AddComboBoxColumnsCurency()
        Dim comboboxColumn As DataGridViewComboBoxColumn
        comboboxColumn = CreateComboBoxColumnCurrency()
        SetAlternateChoicesUsingDataSourceCurrency(comboboxColumn)
        comboboxColumn.HeaderText = "Currency"
        dtgContFee.Columns.Add(comboboxColumn)

        'comboboxColumn = CreateComboBoxColumn()
        'SetAlternateChoicesUsingItems(comboboxColumn)
        'comboboxColumn.HeaderText = _
        '    "TitleOfCourtesy (via Items property)"
        '' Tack this example column onto the end.
        'DataGridView1.Columns.Add(comboboxColumn)
    End Sub

    Private Function CreateComboBoxColumnCurrency() As DataGridViewComboBoxColumn
        Dim column As New DataGridViewComboBoxColumn()

        With column
            .DataPropertyName = "ID_Currency"
            .HeaderText = "Currency"
            .DropDownWidth = 160
            .Width = 90
            .MaxDropDownItems = 3
            .FlatStyle = FlatStyle.Flat
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
        End With
        Return column
    End Function

    Private Sub SetAlternateChoicesUsingDataSourceCurrency(ByVal comboboxColumn As DataGridViewComboBoxColumn)
        Dim cur = From p In db.tblCurrencies
                       Select p
        With comboboxColumn
            .DataSource = cur
            .ValueMember = "ID_Currency"
            .DisplayMember = "Code"
        End With
    End Sub

    'Public Function TaoColumscbb(ByVal colName As String) As DataGridViewComboBoxColumn
    '    Dim cur = From p In db.tblCurrencies
    '                   Select p

    '    Dim cbbCurrency As New DataGridViewComboBoxColumn
    '    cbbCurrency.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
    '    cbbCurrency.Name = colName
    '    cbbCurrency.HeaderText = "Currency"
    '    cbbCurrency.DataSource = cur
    '    cbbCurrency.DisplayMember = "Code"
    '    cbbCurrency.ValueMember = "ID_Currency"
    '    Return cbbCurrency
    'End Function

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dim uc As ucContFee = CType(m_uc, ucContFee)
        uc.BindingNavigator1.Visible = True
        uc.Panel1.Visible = False
        uc.PanelEx1.Visible = True
        uc.KhoiTaodtgListCont(uc.chkExport.Checked, uc.txtFrom.Text, uc.txtTo.Text, m_ID_Cont)
    End Sub
End Class
